#include "Game.h"
#include "InputManager.h"

#include <SDL_opengl.h>
#include <gl\GLU.h>
#include "SDL_image.h"
#include "Color.h"
using namespace EPOC;
Game::Game(void)
{
	
}


Game::~Game(void)
{
}

int Game::Initialize()
{
	GameCore::Initialize();
//	texture = LoadTextureFromFile("d:\\panda.bmp", false, true);
	pandatex = LoadImageToTex("d:\\panda.bmp");
	return 1;
}
int Game::Update()
{
	if(g_inputManager->IsKeyPressed(EPOC::EPKEY_ESCAPE))
		OnExit();


	return 1;

}

void Game::Render()
{

	EPOC::ColorRGB color(100,149,237);
	/* Set the background black */
	glClearColor( color.ToVector3().m_fX, color.ToVector3().m_fY, color.ToVector3().m_fZ, 1.0f );
	/* Clear The Screen And The Depth Buffer */
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

	/* Move Left 1.5 Units And Into The Screen 6.0 */
	glLoadIdentity();
	glTranslatef( -1.5f, 0.0f, -6.0f );

	glBegin( GL_TRIANGLES );            /* Drawing Using Triangles */
	glVertex3f(  0.0f,  1.0f, 0.0f ); /* Top */
	glVertex3f( -1.0f, -1.0f, 0.0f ); /* Bottom Left */
	glVertex3f(  1.0f, -1.0f, 0.0f ); /* Bottom Right */
	glEnd( );                           /* Finished Drawing The Triangle */

	float w,h;

	SDL_Texture* sdl_texture = NULL;
    if(pandatex) 
	
	{
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
        sdl_texture = (SDL_Texture *) pandatex;
        SDL_GL_BindTexture(sdl_texture, &w, &h);
    }

	glEnable(GL_TEXTURE_2D);
		glDisable(GL_ALPHA_TEST);

		glEnable(GL_BLEND);
		glBlendFunc(GL_ONE, GL_ZERO);

	/* Move Right 3 Units */
	glTranslatef( 3.0f, 0.0f, 0.0f );

	glBegin( GL_QUADS );                /* Draw A Quad */
	glTexCoord2f( 0, 0 );
	glVertex3f( -1.0f,  1.0f, 0.0f ); /* Top Left */
	glTexCoord2f( 0, 1 );
	glVertex3f(  1.0f,  1.0f, 0.0f ); /* Top Right */
	glTexCoord2f( 1, 1 );
	glVertex3f(  1.0f, -1.0f, 0.0f ); /* Bottom Right */
	glTexCoord2f( 1, 0 );
	glVertex3f( -1.0f, -1.0f, 0.0f ); /* Bottom Left */
	glEnd( );                           /* Done Drawing The Quad */

	glDisable(GL_ALPHA_TEST);
	
	
	
	
	
	if (pandatex) {
        SDL_GL_UnbindTexture(sdl_texture);
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    }




	SDL_RenderPresent(GetSDLRenderer());
	glFlush();
}


Texture2D* Game::LoadTextureFromFile(const char* sFilePath, bool bExclusive, bool bEditable) 
{
	Texture2D *pTexture = NULL;

	//if(bExclusive == false)
	//	pTexture = GetTexture(sFilePath);

	if(pTexture == NULL)
	{
		SDL_Surface *pImageSurface = IMG_Load(sFilePath);
		if(!pImageSurface)
		{
			printf("GraphicsManager: Error Couldn't load image, %s\n", IMG_GetError());
			// handle error
		}
		else
		{

			pTexture = new Texture2D();
			if( ((Texture2D*)pTexture)->Initialize(sFilePath, sFilePath, pImageSurface, bExclusive, bEditable) == false)
			{
				pTexture->Release();
				pTexture = NULL;
			}

		}
	}
	else
		pTexture->AddRef();

	return pTexture;
}


SDL_Texture* Game::LoadImageToTex(const char* file)
{
	SDL_Surface *loadedImage = nullptr;
	SDL_Texture *tex = nullptr;
	SDL_Renderer* rnd = GetSDLRenderer();
	loadedImage = IMG_Load(file);
	if (loadedImage != nullptr)
	{
		tex = SDL_CreateTextureFromSurface(rnd, loadedImage);
		SDL_FreeSurface(loadedImage);
	}
	else
		_asm int 3
	return tex;
}